package is.glif.fenius.client.draft;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import is.glif.fenius.ws.draft.gen.Avp;
import is.glif.fenius.ws.draft.gen.CommonParams;
import is.glif.fenius.ws.draft.gen.ReservationParams;
import is.glif.fenius.ws.draft.gen.Status;

public class ConsoleOutputter {
	
	int callOrderSingleResv = 1;
	int callOrderMultResvs = 1;
	
    public void printReservations(Map<String, ReservationParams> resvs) {
    	
    	System.out.println("Call order printing multiple reservations: " + callOrderSingleResv++);
    	
        System.out.println("Request output: ");
        for (String key : resvs.keySet()) {
            System.out.println("Request: "+key);
            printReservation(resvs.get(key));
        }

    }
    private void printReservation(ReservationParams resv) {
    	
    	System.out.println("Call order printing multiple reservations: " + callOrderMultResvs++);

        /*
        JSONObject jResponse = new JSONObject();


        jResponse = JSONObject.fromObject(resv);


        String output = "";
        try {
            output = jResponse.toString(2);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        System.out.println(output);
        System.exit(1);
        */
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CommonParams cp = resv.getEndtoendParams();
        Integer bandwidth = cp.getBandwidth();
        List<Avp> adhoc = cp.getAdhocParams().getAvp();
        String src = resv.getEndpoints().getA();
        String dst = resv.getEndpoints().getZ();
        Status status = cp.getStatus();
        String gid = resv.getReservationId();
        String reqStart = "N/A";
        String reqEnd = "N/A";
        String schStart = "N/A";
        String schEnd = "N/A";

        if (cp.getTimeParams().getRequestedStartTime() != null) {
            reqStart = df.format(cp.getTimeParams().getRequestedStartTime().toGregorianCalendar().getTime());
        }
        if (cp.getTimeParams().getRequestedEndTime() != null) {
            reqEnd = df.format(cp.getTimeParams().getRequestedEndTime().toGregorianCalendar().getTime());
        }
        if (cp.getTimeParams().getScheduledStartTime() != null) {
            schStart = df.format(cp.getTimeParams().getScheduledStartTime().toGregorianCalendar().getTime());
        }
        if (cp.getTimeParams().getScheduledEndTime() != null) {
            schEnd = df.format(cp.getTimeParams().getScheduledEndTime().toGregorianCalendar().getTime());
        }

        System.out.println("  resv id: "+gid);
        System.out.println("  status: "+status.toString());
        System.out.println("  src: "+src);
        System.out.println("  dst: "+dst);
        System.out.println("  bw: "+bandwidth);
        System.out.println("  time params:");
        System.out.println("    req-start: "+reqStart);
        System.out.println("    req-end: "+reqEnd);
        System.out.println("    sch-start: "+schStart);
        System.out.println("    sch-end: "+schEnd);
        System.out.println("  ad-hoc params:");
        for (Avp avp : adhoc) {
            System.out.println("    "+avp.getAttrName()+" : "+avp.getAttrVal());
        }

    }

}
